using System.Windows.Forms;

namespace StreamReadWrite
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void browseButton1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileTextBox1.Text = openFileDialog1.FileName;
            }
        }
        private void browseButton2_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                fileTextBox2.Text = openFileDialog2.FileName;
            }
        }
        private void mergeButton_Click(object sender, EventArgs e)
        {
            string file1 = fileTextBox1.Text;
            string file2 = fileTextBox2.Text;

            if (string.IsNullOrWhiteSpace(file1) || string.IsNullOrWhiteSpace(file2))
            {
                MessageBox.Show("Please select both files.");
                return;
            }

            try
            {
                string directory = Path.Combine(Application.StartupPath, "Data");
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                string mergedFilePath = Path.Combine(directory, "merged.txt");

                using (StreamWriter writer = new StreamWriter(mergedFilePath))
                {
                    writer.WriteLine("Contents of File 1:");
                    writer.WriteLine(File.ReadAllText(file1));
                    writer.WriteLine();
                    writer.WriteLine("Contents of File 2:");
                    writer.WriteLine(File.ReadAllText(file2));
                }

                MessageBox.Show("Files merged successfully. Merged file saved at:\n" + mergedFilePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occurred while merging files: " + ex.Message);
            }
        }

        
    }
}
